/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.ekuma.data.ui.swing;

import com.openbravo.data.basic.BasicException;
import com.openbravo.data.user.DirtyManager;
import java.awt.Component;
import java.util.Properties;

import javax.swing.JPanel;

/**
 *
 * @author Administrator
 */
public abstract class AbstractDAOJEditor<T> extends javax.swing.JPanel implements SwingEditorRecord<T>{
    protected T obj;
    
    protected int state=STATE_EOF;

    protected DirtyManager dirty;

    public AbstractDAOJEditor(DirtyManager dirty) {
        this.dirty = dirty;
    }

    public Component getComponent() {
        return this;
    }

    public void writeValueInsert(Properties prop) {
        state=STATE_Insert;
        obj=createNew(prop);
        updateUI(true,state);
    }

    public void writeValueDelete(T value) {
       state=STATE_Delete;
       obj=value;
       updateUI(false,state);
    }

    public void writeValueEOF() {
        state=STATE_EOF;
        obj=createNew(new Properties());
        updateUI(false,state);
    }

    public void writeValueEdit(T value) {
       state=STATE_Edit;
       obj=value;
       updateUI(true,state);
    }

    public T createValue() throws BasicException {
        if(dirty.isDirty())
            rebuild(obj);
        String retMsg=validator(obj);
        if(retMsg!=null)
            throw new BasicException(retMsg);
        return obj;
    }

    public abstract T createNew(Properties prop);

    public abstract void rebuild(T obj)throws BasicException ;

    public abstract void updateUI(boolean editAble,int state);

    public String validator(T obj){
        return null;
    }

    public DirtyManager getDirty() {
        return dirty;
    }

    public void setDirty(DirtyManager dirty) {
        this.dirty = dirty;
    }

    public int getState() {
        return state;
    }

    public abstract Class getEditClass();

    public void activate() throws BasicException{
        
    }

	@Override
	public String getLogerString() {
		return null;
	}

	@Override
	public boolean isLogAble() {
		return false;
	}

	public void hostSelected(T item) {
	}

	public void configTotalPanel(JPanel extPanel) {
		// TODO Auto-generated method stub
		
	}

}
